package com.creative.core.controller;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.http.HttpUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.aliyuncs.exceptions.ClientException;
import com.creative.core.domain.AjaxResult;
import com.creative.core.uitls.oss.AliOssUtil;
import com.creative.core.uitls.security.MpSecurity;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.creative.constant.Constants.CACHE_ACCESS_TOKEN_KEY;

/**
 * 文件上传控制器
 * 创建人: ling
 * 创建时间: 2025/03/06
 */
@RestController
@RequestMapping(value = "/file")
public class FileController {
    public static final Log logger = LogFactory.get();

    @Resource
    private AliOssUtil aliOssUtil;

    /**
     * 上文件传
     *
     * @param file 文件
     * @return {@link AjaxResult }
     * @author ling
     * @date 2025/03/06
     */
    @PostMapping("/upload")
    public AjaxResult fileUpload(@RequestParam("file") MultipartFile file) {
        AjaxResult ajaxResult = AjaxResult.success();
        logger.info("文件上传开始:{}", file);
        //        调用oss的文件上传方法
        try {
            //原始文件名字
            String originalFilename = file.getOriginalFilename();
            //截取原始文件名后缀
            int lastIndexOf = originalFilename.lastIndexOf(".");
            String extension = originalFilename.substring(lastIndexOf);
            //构造新文件的名称
            String objectName = UUID.randomUUID().toString() + extension;
            //        文件上传后返回的路径
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);

            return ajaxResult.put("filePath", filePath);
        } catch (IOException | ClientException e) {
            logger.error("文件上传失败：{}", e);
        }
        return AjaxResult.error("文件上传失败");
    }

    /**
     * 小程序文件上传
     *
     * @param file 文件
     * @return {@link AjaxResult }
     * @author ling
     * @date 2025/05/12
     */
    @PostMapping("/mp/upload")
    public AjaxResult mpFileUpload(@RequestParam("file") MultipartFile file) {
        AjaxResult ajaxResult = AjaxResult.success();
        try {
            //原始文件名字
            String originalFilename = file.getOriginalFilename();
            //截取原始文件名后缀
            int lastIndexOf = originalFilename.lastIndexOf(".");
            String extension = originalFilename.substring(lastIndexOf);
            //构造新文件的名称
            String objectName = UUID.randomUUID().toString() + extension;
            //        文件上传后返回的路径
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
            return ajaxResult.put("filePath", filePath);
        } catch (IOException | ClientException e) {
            logger.error("文件上传失败：{}", e);
        }
        return AjaxResult.error("文件上传失败");
    }

}
